package org.cncy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;

/**
 * 登录接口
 */
import java.io.Serializable;
import java.util.Random;

public class I3001JavaSampler extends AbstractJavaSamplerClient implements Serializable {
    private static Logger log = LoggerFactory.getLogger(I3001JavaSampler.class);
    private static final long serialVersionUID = 240l;

    private static final String Imei = "Imei";
    private static final String URL = "url";
    private static final String USERNMAE = "userName";
    private static final String PWD = "pwd";
    private static final String TENANTID = "TenantID";


    private static String[] num = {"SM-N976N", " PGCM10", " SM-N976N", " 2211133C", " M2102K1AC", " V2278A", " 2201123C", " PHW110", " 2106118C", " MAA-AN00", " 23127PN0CC", " V2352A", " STG-AL00", " ALP-AN00", " MAA-AN00", " SM-S916N", " 23013RK75C", " V2339A", " M2012K11C", " V2057A", " SM-S9260", " PFVM10", " 22101320C", " V2279A", " V1914A", " NAM-AL00", " SM-S9180", " M2007J3SC", " V2055A", " M2103K19C", " SM-S928N", " 23127PN0CC", " SM-F721N", " SM-S711N", " SM-S711N", " SM-S901N", " SM-F711N", " SM-S9280", " BVL-AN16", " V2285A", " ALT-AN00", " LYA-AL00", " V2154A", " V2364A", " M2007J17C", " SM-S928N", " SEA-AL00", " 21091116AC", " V2359A", " LNA-AL00", " SM-S9210", " PFTM20", " PCLM50", " V2314A", " NTN-AN20", " LRA-AN00", " SM-S928N", " TET-AN50", " 24053PY09C", " SM-G986N", " 2211133C", " SM-A5560", " V2329A", " SM-G7810", " BVL-AN00", " ALN-AL00", " SM-S9280", " PEMM00", " ELP-AN00", " ALN-AL00", " SM-F721N", " PPA-AL20", " 22101317C", " ALN-AL00", " SM-S9180", " V2309A", " CDY-AN00", " KOZ-AL00", " BRA-AL00", " V2329A", " PJA110", " ADY-AL00", " VNE-AN00", " SM-S9280", " HPB-AN00", " 23117RK66C", " V2364A", " BVL-AN00", " MNT-BD00", " 24053PY09C", " MAA-AN10", " PEQM00", " SM-N981N", " Hera-BD00", " 23113RKC6C", " 23127PN0CC", " M2004J7AC", " 2211133C", " V2068A", " PJV110", " MXW-AN00", " V2338A", " 23127PN0CC", " SM-S9280", " SM-F711N", " 2211133C", " SM-N976N", " DUK-AL20", " NOH-AN01", " V1838A", " MAA-AN00", " HBN-AL00", " SM-F7310", " PDCM00", " V1938CT", " SM-F926N", " ALN-AL00", " PJE110", " 23127PN0CC", " SM-S9280", " PEGM10", " SM-G991N", " PHM110", " SM-S901N", " SM-S9080", " PGT-AN00", " SM-A346N", " ALI-AN00", " SM-N976N", " 2312DRAABC", " LIO-AN00", " BVL-AN16", " RMO-AN00", " SM-S906N", " SM-G991N", " 2312DRA50C", " SM-N981N", " ALI-AN00", " V2307A", " SM-S926N", " CDY-AN20", " SM-S928N", " V1913A", " TAS-AL00", " 23116PN5BC", " V2303A", " V2338A", " 22101316UCP", " PJW110", " SM-S9280", " 2211133C", " YAL-AL10", " LSA-AN00", " HBN-AL10", " 23113RKC6C", " ALI-AN00", " V2170A", " PJE110", " SM-S9160", " PJE110", " SM-S9010", " V2121A", " BVL-AN00", " SM-N986N", " IN2020", " PEMM00", " SM-S9260", " ALN-AL00", " SM-F946N", " SM-N986N", " PGFM10", " 2211133C", " LNA-AL00", " ELP-AN00", " ALN-AL80", " V2285A", " MAA-AN10", " ADY-AL00", " REP-AN00", " ELS-AN00", " V2166A", " CMA-AN00", " Mi 10", " V2059A", " 23117RK66C", " V2359A", " ALN-AL00", " 2311DRK48C", " FRL-AN00a", " ALN-AL10", " V2324A", " SM-S9010", " SM-F731N", " SM-S928N", " ALI-AN00", " V1965A", " PFUM10", " TEL-AN10", " SM-S918N", " 23113RKC6C", " V1911A", " PEPM00", " PEQM00", " 2312DRAABC", " 22041211AC", " WDY-AN00", " SM-A5460", " RMX3770", " SM-G977N", " LIO-AN00", " SM-S928N", " SM-S9260", " V2314A", " SM-S918N", " HBN-AL00", " 2311DRK48C", " V2239A", " STG-AL00", " PGAM10", " Redmi K20 Pro", " V2036A", " SM-S9180", " SM-F7310", " V2318A", " ADY-AL00", " RMX3121", " V2148A", " M2103K19C", " KOZ-AL00", " SEA-AL10", " V1818A", " MI 8", " V2154A", " V2185A", " 23049RAD8C", " PJV110", " 22041216C", " NTH-AN00", " WLZ-AN00", " V2324HA", " BVL-AN16", " M2007J22C", " V2283A", " PJA110", " 21091116C", " SM-S908N", " MI 8 SE", " MAA-AN10", " FIN-AL60", " ELS-AN00", " SM-S9280", " HMA-AL00", " FOA-AL00", " SM-S9260", " 2211133C", " SM-S9260", " SM-S906N", " V2338A", " SM-S921N", " LNA-AL00", " PERM10", " Redmi K20 Pro Premium Edition", " ADY-AL00", " 24031PN0DC", " BVL-AN16", " 2211133C", " V2359A", " V2338A", " MAA-AN00", " V2242A", " SM-S9260", " LIO-AN00", " PEMM20", " V2123A", " V2338A", " PEQM00", " SM-S9260", " 23116PN5BC", " SM-S921N", " 23127PN0CC", " FIN-AL60", " V2301A", " PGFM10", " 2211133C", " V2324HA", " JEF-AN00", " SM-S9260", " V2307A", " SM-A5560", " CDY-AN95", " 23013RK75C", " ADA-AL10U", " BVL-AN16", " SM-S9280", " 22041211AC", " 2312DRAABC", " SM-A5360", " SM-S9280", " 2211133C", " RTE-AL00", " KOZ-AL40", " SM-S9080", " ELS-AN00", " M2006J10C", " V2359A", " 23127PN0CC", " 23113RKC6C", " 23127PN0CC", " 22081212C", " V2272A", " PGKM10", " SM-S908N", " V1829A", " 2211133C", " V1913A", " SM-S9180", " JEF-AN00", " SM-S9010", " 23127PN0CC", " V2162A", " 23116PN5BC", " 2206123SC", " SM-S9280", " 22081212C", " SM-G9860", " MAA-AN10", " PJB110", " OXF-AN10", " PHJ110", " 23127PN0CC", " 2201123C", " ELI-AN00", " RNA-AN00", " V2334A", " 23116PN5BC", " 2201123C", " ALP-AN00", " SM-S911N", " SM-S911N", " SM-N981N", " 2304FPN6DC", " 24031PN0DC", " PFVM10", " SM-A5260", " SM-S908N", " SM-S9280", " HBP-AL00", " FRI-AN10", " DCO-AL00", " PJA110", " ALI-AN00", " V1813A", " SM-S9210", " RMX3617", " 23113RKC6C", " RTE-AL00", " 23127PN0CC", " SM-S9180", " SM-S9210", " V2054A", " TAS-AN00", " PDVM00", " V2072A", " 2211133C", " PFZM10", " ADY-AL00", " PHT110", " 21121119SC", " 23113RKC6C", " SM-F721N", " Mi 10 Pro", " M2012K11AC", " RNA-AN00", " 23117RK66C", " ELS-AN00", " V2001A", " LIO-AN00", " REA-AN00", " NOH-AN00", " GLA-AL00", " NOH-AL10", " M2012K11AC", " PJE110", " PGZ110", " 23078RKD5C", " SM-S9080", " M2012K11AC", " PJD110", " HBN-AL00", " M2102J2SC", " SM-S9080", " PHY110", " SM-S928N", " ELI-AN00", " ANA-AN00", " Hera-BD00", " SM-S9280", " PJV110", " ANG-AN00", " SM-S911N", " SM-F731N", " 24031PN0DC", " PFUM10", " M2011K2C", " PJD110", " ADY-AL00", " PDYM20", " LLY-AN00", " KOZ-AL40", " ALI-AN00", " 2312DRAABC", " PJD110", " SM-S928N", " ELS-AN00", " 23113RKC6C", " FIN-AL60a", " 23127PN0CC", " MAG-AN00", " HBN-AL10", " NTH-AN00", " ADT-AN00", " 23113RKC6C", " SM-S928N", " HBN-AL10", " JEF-AN00", " V2359A", " SM-S911N", " V2364A", " SM-C5560", " 24031PN0DC", " SM-S9280", " 2211133C", " V2343A", " M2010J19SC", " CDY-AN00", " PJD110", " 2312DRA50C", " V2136A", " Redmi K30i 5G", " SM-S9280", " SM-F731N", " SM-N960N", " SM-S918N", " JEF-AN20", " PJV110", " 2210132C", " SM-S9280", " vivo X20A", " HLK-AL00", " TEL-AN10", " V2156FA", " TAS-AN00", " PHJ110", " PJD110", " 23116PN5BC", " JNY-AL10", " V2203A", " V1818CA", " ANA-AN00", " CDY-AN90", " PJE110", " ADY-AL00", " V2073A", " SM-S9280", " V2049A", " V2361A", " RNA-AN00", " PHK110", " SM-S9280", " 21091116AC", " ADY-AL00", " V1838A", " V2361A", " 21091116AC", " WLZ-AN00", " V2157A", " IN2010", " SM-S9260", " V2196A", " 23127PN0CC", " 23113RKC6C", " SM-S9280", " V2054A", " 23127PN0CC", " 23113RKC6C", " SM-F946N", " SM-S911N", " V2353A", " PDYM20", " V1965A", " V2230A", " PHM110", " HLK-AL00", " ALI-AN00", " PJF110", " PHU110", " 2304FPN6DC", " ELS-AN00", " 2312DRAABC", " V2304A", " RMX3888", " 22041211AC", " TAS-AN00", " BVL-AN00", " SM-F9360", " BVL-AN00", " PBEM00", " V2057A", " AGT-AN00", " V2339A", " 23013RK75C", " PGT-AN20", " 23116PN5BC", " SM-S918N", " ELS-AN00", " TDT-MA01", " V2220A", " SM-G991N", " SM-A528N", " SM-S911N", " V2359A", " PHK110", " BNE-AL00", " BVL-AN00", " SM-S928N", " ELI-AN00", " SM-F7310", " SM-S9280", " M2012K11AC", " SM-F936N", " WDY-AN00", " PJE110", " SM-S926N", " JEF-AN00", " SM-S9280", " PHY110", " V2148A", " SM-S9260", " PEFM00", " V2278A", " SM-S918N", " SM-S921N", " SM-S9280", " ALP-AN00", " ADA-AL00", " ADT-AN00", " CET-AL60", " V2307A", " 23113RKC6C", " SM-S921N" };

    public I3001JavaSampler() {
        log.debug(Utils.whoAmI(this) + "\tConstruct");
    }


    //通过jmter的图形框进行传参,无默认值
    @Override
    public Arguments getDefaultParameters() {
        Arguments arguments = new Arguments();
        arguments.setComment("登录接口");
        arguments.addArgument(Imei, "");
        arguments.addArgument(URL, "");
        arguments.addArgument(USERNMAE, "");
        arguments.addArgument(PWD, "");
        arguments.addArgument(TENANTID, "");

        return arguments;
    }

    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult results = new SampleResult();
        //返回请求值，体现在jmeter查看结果数请求里面
        String imei = javaSamplerContext.getParameter(Imei);
        String url = javaSamplerContext.getParameter(URL);
        String userName = javaSamplerContext.getParameter(USERNMAE);
        String pwd = javaSamplerContext.getParameter(PWD);
        String tenantID = javaSamplerContext.getParameter(TENANTID);

        log.debug("Imei = {},url = {},userName = {},pwd = {},tenantID = {}", imei, url, userName, pwd, tenantID);
        // 密码加密
        String authPwd = DesEncryptUtil.encrypt(pwd);

        String Model = num[new Random().nextInt(num.length)];
        int osVersion = new Random().nextInt(14 - 10) + 9;
        String SN = Utils.getRandomStrMin(16).toString();
        String body = "<?xml version='1.0' encoding='UTF-8' ?><Request><OperID><TenantID>"+tenantID+"</TenantID><ClientUID></ClientUID><FlowNum></FlowNum><Timezone>-1</Timezone><Result>-1</Result><ClientVersion>3.2.8</ClientVersion><ClientBuildNum>328</ClientBuildNum></OperID><ClientIDInfo><UserName>"+ userName +"</UserName><UserPassword>"+ authPwd + "</UserPassword><EMMClientId></EMMClientId></ClientIDInfo><MobileIDInfo><ClientOSType>1</ClientOSType><ClientOS>Android</ClientOS><ModelType>1</ModelType><Model>"+ Model +"</Model><OSVersion>"+osVersion+"</OSVersion><Jailed>0</Jailed><Imei>" + imei + "</Imei><WifiMac>68:13:24:34:0a:ee</WifiMac><Imsi>460065017196611/</Imsi><SN>"+ SN + "</SN><ClientIP>192.168.43.35</ClientIP><SdID></SdID><SpecialTypeFlag>0</SpecialTypeFlag><manufacturerOSVersion>EMUI_EmotionUI_11.0.0</manufacturerOSVersion></MobileIDInfo></Request>";
        results.setSamplerData("请求体 : " + body);
        results.sampleStart();
        try {
            byte[] bytes = HttpsUtils.doPost(url, HttpsXMLUtil.getXMLRequestByte(3001, body));
            // byte[] bytes1 = Utils.interceptBody(bytes);
            results.setSuccessful(true);//这里决定测试用例成功还是失败
            //设置返回信息
            results.setResponseCode("200");
            results.setResponseMessage("成功测试");
            //results.setSampleLabel("用户登录");
            //返回结果
            results.setResponseData(new String(Utils.interceptBody(bytes)), "utf-8");
        } catch (Exception e) {
            log.error("Exception = {}", e);
            results.setSuccessful(false);
        } finally {
            // Record end time and populate the results.
            results.sampleEnd();
        }
        if (log.isDebugEnabled()) {
            log.debug(Utils.whoAmI(this) + "\tTime:\t" + results.getTime());
        }

        return results;
    }

}
